<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );
class Agent_model extends MY_Model {
	var $table = 'agent';
	public function __construct() {
		parent::__construct ();
	}
	public function getAgentAll($address_id, $role='') {
		$this->db->select ( 'a.id, a.username, a.nickname' )
		->from ( $this->table . ' a')
		->join('agent_assignment aa', 'a.id=aa.agent_id', 'left')
		->where (array('a.status'=>REST_Controller::STATUS_ACTIVE, 'aa.status'=>REST_Controller::STATUS_ACTIVE, 'aa.address_id'=>$address_id));
		if(!empty($role)) {
			$this->db->where(array('role'=>$role));
		}
		$query = $this->db->get ();
		return $query->result_array ();
	}
	public function getDiliverierAll($address_id, $config=array()) {
		$limit = NULL;
		$offset = NULL;		
		if (!empty($config)) {
			foreach ($config as $key => $val) {
				$$key = $val;
			}
		}
		if ($limit && $offset >= 0) {
			$this->db->limit($limit, $offset);
		}
		$this->db->select ( 'a.id, a.username, i.realname, a.status, al.img' )
		->from ( $this->table . ' a')
		->join('agent_assignment aa', 'a.id=aa.agent_id', 'left')
		->join('identification i', 'a.id=i.agent_id', 'left')
		->join('album al', 'i.autodyne_id=al.id', 'left')
		->where(array('role'=>REST_Controller::ROLE_DILIVERIER, 'a.status >'=>REST_Controller::STATUS_DELETE))
		->where_in ('aa.address_id', $address_id);
		$query = $this->db->get ();
		return $query->result_array ();
	}
	public function addOne($data, $data2, $data3) {
		$this->db->trans_start();
		$this->db->insert($this->table, $data);
		$id = $this->db->insert_id();
		$data2['agent_id'] = $id;
		$data3['agent_id'] = $id;
		$this->db->insert('identification', $data2);
		$this->db->insert('agent_assignment',$data3);
		$this->db->trans_complete();
		return $this->db->trans_status()?$id:NULL;
	}
	public function updateOne($data, $id) {
		$this->db->trans_start();
		$this->db->update('identification', $data, array('agent_id'=>$id));
		$this->update(array('status'=>REST_Controller::STATUS_PROCESSING), array('id'=>$id));
		$this->db->trans_complete();
		return $this->db->trans_status();
	}
	public function unlinkImages($id) {
		$query = $this->db->select('id,img')
				->from('album')
				->where(array('source_id'=>$id,'from'=>'4','type'=>'1'))->get();
		$data = $query->result_array();
		if(!empty($data)) {
			$ids = array();
			foreach ($data as $v) {
				$ids[] = $v['id'];
			}
			$this->db->where_in('id', $ids);
			$this->db->delete('album');
		}
		return $data;
	}
	public function getAddressId($id) {
		$query = $this->db->select('address_id')
				->from('agent_assignment')
				->where(array('agent_id'=>$id))
				->get();
		$rows = $query->result_array();
		$data = array();
		if(!empty($rows)) {
			foreach ($rows as $v) {
				$data[] = $v['address_id'];
			}
		}
		return $data;
	}
	public function updateAssignment($data, $where) {
		foreach ($where as $k=>$v) {
			if(is_array($v)) {
				$this->db->where_in($k, $v);
			} else {
				$this->db->where($k, $v);
			}
		}
		return $this->db->update('areas_enabled', $data);
	}
	public function assignment_status($address_id) {
		$query =$this->db->select('ae.status,concat_ws(\' \',a.description, ae.address) as contact_address',false)->from('areas_enabled ae')
				->join('areas a', 'ae.district_id=a.id', 'left')
				->where('ae.id', $address_id)
				->limit(1)->get();
		return $query->row_array();
	}
}